package com.wpre.lotterysystem.controller.handler;

import com.wpre.lotterysystem.common.errcode.GlobalErrorCodeConstants;
import com.wpre.lotterysystem.common.exception.ControllerException;
import com.wpre.lotterysystem.common.exception.ServiceException;
import com.wpre.lotterysystem.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-15
 * Time: 8:45
 */
@Slf4j
@RestControllerAdvice   // 这个注解可以捕获全局抛的异常
public class GlobalExceptionHandler {
    //接下来可以指定捕获的异常交给谁处理

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> ServiceException(ServiceException e) {
        e.printStackTrace();
        //打印日志
        log.error("出现ServiceException");

        //构造错误结果
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMsg());
    }


    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> ControllerException(ControllerException e) {
        //打印日志
        log.error("出现ControllerException");
        //构造错误结果
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMsg());
    }

    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> Exception(Exception e) {
        e.printStackTrace();
        //打印日志
        log.error("出现服务异常");
        //构造错误结果
        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMessage());
    }


}
